package com.basic.modules.ykf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.TypeReference;
import com.basic.common.utils.ParamsModel;
import com.basic.common.utils.ParamUtils;
import com.basic.modules.sys.entity.SysUserEntity;
import com.basic.modules.sys.model.EmployeeManagementExcel;
import com.basic.modules.ykf.model.ExcelReq;
import com.basic.modules.ykf.model.MsgRecordExcel;
import com.basic.modules.ykf.utils.ExcelCellWidthStyleStrategy;
import com.basic.modules.ykf.utils.ExcelStyleUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basic.common.utils.PageUtils;
import com.basic.common.utils.Query;

import com.basic.modules.ykf.dao.YkfMsgRecordDao;
import com.basic.modules.ykf.entity.YkfMsgRecordEntity;
import com.basic.modules.ykf.service.YkfMsgRecordService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;


@Service("ykfMsgRecordService")
public class YkfMsgRecordServiceImpl extends ServiceImpl<YkfMsgRecordDao, YkfMsgRecordEntity> implements YkfMsgRecordService {

    @Override
    public PageUtils queryPage(ParamsModel<YkfMsgRecordEntity> params) {
        QueryWrapper<YkfMsgRecordEntity> queryWrapper = ParamUtils.createQueryWrapperByDefaultSort(params);
        if (ObjectUtil.isNotEmpty(params.getCondition())) {
            if (StrUtil.isNotBlank(params.getCondition().getKeyword())) {
                queryWrapper.lambda().eq(YkfMsgRecordEntity::getMobile, params.getCondition().getKeyword());
            }
            if (StrUtil.isNotBlank(params.getCondition().getDateStart())) {
                queryWrapper.lambda()
                        .and(wrapper -> wrapper.ge(YkfMsgRecordEntity::getRecordTime, params.getCondition().getDateStart())
                                .or().ge(YkfMsgRecordEntity::getSendTime, params.getCondition().getDateStart())
                                .or().ge(YkfMsgRecordEntity::getPreSendTime, params.getCondition().getDateStart()));
            }
            if (StrUtil.isNotBlank(params.getCondition().getDateEnd())) {
                queryWrapper.lambda()
                        .and(wrapper -> wrapper.le(YkfMsgRecordEntity::getRecordTime, params.getCondition().getDateEnd())
                                .or().le(YkfMsgRecordEntity::getSendTime, params.getCondition().getDateEnd())
                                .or().le(YkfMsgRecordEntity::getPreSendTime, params.getCondition().getDateEnd()));
            }
        }
        IPage<YkfMsgRecordEntity> page = this.page(
                new Query<YkfMsgRecordEntity>().getPage(BeanUtil.beanToMap(params.getPage())),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void smsLog(ExcelReq req, HttpServletResponse response) throws IOException {
        List<MsgRecordExcel> list = new ArrayList<>();
        //查询列表
        ParamsModel<YkfMsgRecordEntity> params = new ParamsModel<>();
        params.setCondition(req.getCondition(new TypeReference<YkfMsgRecordEntity>() {}));
        params.setSort(req.getSort());
        ParamsModel.PageParams pageParams = new ParamsModel.PageParams();
        pageParams.setIndex(1L);
        pageParams.setSize(9999L);
        params.setPage(pageParams);
        PageUtils pageUtils = queryPage(params);
        List<YkfMsgRecordEntity> dataList = (List<YkfMsgRecordEntity>) pageUtils.getList();
        if (ObjectUtil.isNotEmpty(dataList)) {
            for (YkfMsgRecordEntity record : dataList) {
                MsgRecordExcel excel = new MsgRecordExcel();
                BeanUtil.copyProperties(record, excel);
                if (ObjectUtil.isNotEmpty(record.getStatus())) {
                    excel.setStatus(record.getStatus() == 1 ? "已发送" : "未发送");
                }
                list.add(excel);
            }
        }
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("短信日志", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(ExcelStyleUtils.getHeadStyle(), ExcelStyleUtils.getContentStyle());
        ExcelCellWidthStyleStrategy widthStyleStrategy = new ExcelCellWidthStyleStrategy();
        EasyExcel.write(response.getOutputStream(), MsgRecordExcel.class)
                .sheet("短信日志")
                .registerWriteHandler(horizontalCellStyleStrategy)
                .registerWriteHandler(widthStyleStrategy)
                .doWrite(list);
    }

}
